<?php
/*
	Copyright 2006, 2007, 2008, 2009, 2010 Bastiaan Grutters
    
    This file is part of Ages of Strife website.

    Ages of Strife website is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Ages of Strife website is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Ages of Strife website.  If not, see <http://www.gnu.org/licenses/>.
 */
include( '../global/initialize.php' );

aos_header( 'Visible armies' );

$type = "AND travel > 0 ";
if( isset( $_GET ) && isset( $_GET[ 'view' ] ) && strtolower( $_GET[ 'view' ] ) == 'all' ) {
	$type = "";
}
$query2 = "SELECT faction_id FROM faction_member WHERE ruler_id = " . $_SESSION[ 'ruler_id' ] . " AND requested = 0 AND invited = 0";
$result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
$row = mysql_fetch_array( $result2, MYSQL_ASSOC );
$faction_id = -1;
if( isset( $row[ 'faction_id' ] ) ) {
	$faction_id = $row[ 'faction_id' ];
    $query = "SELECT DISTINCT army.*" .
    		"FROM army " .
    		"JOIN army_visible ON army_visible.army_id = army.army_id " .
    		"WHERE scout + infantry + cavalry + artillery > 0 " . $type .
    		"AND army_visible.ruler_id IN ( SELECT DISTINCT ruler_id FROM faction_member WHERE faction_id = " . $row[ 'faction_id' ] . " AND requested = 0 AND invited = 0 ) " .
    		"ORDER BY actual_y, actual_x, name";
}
else {
    $query = "SELECT army.* " .
    		"FROM army " .
    		"JOIN army_visible ON army_visible.army_id = army.army_id " .
    		"WHERE army_visible.ruler_id = " . $_SESSION[ 'ruler_id' ] . " AND scout + infantry + cavalry + artillery > 0 " . $type .
    		"ORDER BY actual_y, actual_x, name";
}
$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
$num = mysql_numrows( $result );
?>
					<div class="blok">
<?php
print_operations_tab( 'Military activity' );
?>
					</div>
					<br/>
					<div class="blok">
<?php
if( $type == '' ) {
    print( translate( "View" ) . ": <a href=\"movement.php\">" . translate( "Show only moving armies" ) . "</a>\r\n" );
}
else {
    print( translate( "View" ) . ": <a href=\"movement.php?view=all\">" . translate( "Show stationed and moving armies" ) . "</a>\r\n" );
}
?>
					</div>
					<br />
<?php
if( $num > 0 ) {
?>
	    			<table>
	    				<th colspan="6"><?php transprint( 'Military activity' ); ?></th>
						<tr>
							<td><?php transprint( 'Name' ); ?></td>
							<td><?php transprint( 'Owner' ); ?></td>
							<td><?php transprint( 'Faction' ); ?></td>
							<td><?php transprint( 'Location' ); ?></td>
							<td align="right"><?php transprint( 'Size' ); ?></td>
							<td><?php transprint( 'Direction' ); ?></td>
						</tr>
<?php
    $i = 0;
    while ( $i < $num ) {
        $target = "";
	    if( mysql_result( $result, $i, 'travel' ) == 0 ) {
	    	$target_x = mysql_result( $result, $i, "x" );
	    	$target_y = mysql_result( $result, $i, "y" );
	    }
	    else {
	    	$target_x = round( mysql_result( $result, $i, "actual_x" ) );
	    	$target_y = round( mysql_result( $result, $i, "actual_y" ) );
	    }

	    $alert = "";
        if( mysql_result( $result, $i, "ruler_id" ) == $_SESSION[ 'ruler_id' ] ) {
			$colour =  "<strong class=\"green\">";
        }
        else {
		    $query2 = "SELECT faction_id " .
		    		"FROM faction_member " .
		    		"WHERE ruler_id = " . mysql_result( $result, $i, "ruler_id" ) . " AND invited = 0 AND requested = 0";
		    $result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
		    $row = mysql_fetch_array( $result2, MYSQL_ASSOC );
			if( isset( $row[ 'faction_id' ] ) && $faction_id == $row[ 'faction_id' ] ) {
				$colour =  "<strong class=\"blue\">";
			}
			else {
				$colour =  "<strong class=\"red\">";
			    
			    if( $faction_id == -1 ) {
				    $query2 = "SELECT count(*) " .
				    		"FROM village " .
				    		"WHERE x > $target_x - 5 AND x < $target_x + 5 AND y > $target_y - 5 AND y < $target_y + 5 AND ruler_id = " . $_SESSION[ 'ruler_id' ];
				    $result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
				    $row = mysql_fetch_array( $result2, MYSQL_ASSOC );
				    if( $row[ 'count(*)' ] > 0 ) {
				    	$alert = " under-attack";
				    }
			    }
			    else {
				    $query2 = "SELECT count(*) " .
				    		"FROM village " .
				    		"WHERE x > $target_x - 5 AND x < $target_x + 5 AND y > $target_y - 5 AND y < $target_y + 5 " .
				    		"AND ruler_id IN ( SELECT ruler_id FROM faction_member WHERE faction_id = $faction_id AND requested = 0 AND invited = 0 )";
				    $result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
				    $row = mysql_fetch_array( $result2, MYSQL_ASSOC );
				    if( $row[ 'count(*)' ] > 0 ) {
				    	$alert = " under-attack";
				    }
			    }
			}
        }
    	
        if( $i % 2 == 0 ) {
            print "<tr class=\"even$alert\">";
        }
        else {
            print "<tr class=\"uneven$alert\">";
        }

        $size = mysql_result( $result, $i, "scout" ) + mysql_result( $result, $i, "infantry" ) + mysql_result( $result, $i, "artillery" ) + mysql_result( $result, $i, "cavalry" );
        
        //print( "<tr>" );
	
		if( mysql_result( $result, $i, "ruler_id" ) == $_SESSION[ 'ruler_id' ] ) {
			$army_name = "<a href=\"../armies/army.php?name=" . mysql_result( $result, $i, "name" ) . "\" title=\"" . mysql_result( $result, $i, "name" ) . "\">$colour" . mysql_result( $result, $i, "name" ) . "</strong></a>";
		}
		else {
			$army_name = mysql_result( $result, $i, "name" );
		}
		
	    $query2 = "SELECT faction_id FROM faction_member WHERE ruler_id = " . mysql_result( $result, $i, "ruler_id" ) . " AND invited = 0 AND requested = 0";
	    $result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
	    $row = mysql_fetch_array( $result2, MYSQL_ASSOC );
		if( isset( $row[ 'faction_id' ] ) ) {
		    $query2 = "SELECT tag, name FROM faction WHERE faction_id = " . $row[ 'faction_id' ];
		    $result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
		    $row = mysql_fetch_array( $result2, MYSQL_ASSOC );
			$faction_tag = "<a href=\"../overview/faction.php?name=" . $row[ 'name' ] . "\">$colour" . "[" . strtoupper( $row[ 'tag' ] ) . "]</strong></a>";
		}
		else {
			$faction_tag = "";
		}	
	    
	    $query2 = "SELECT name, ruler_id FROM village WHERE x = '" . $target_x . "' AND y = '" . $target_y . "'";
	    $result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
	    $row = mysql_fetch_array( $result2, MYSQL_ASSOC );
	    $target = "";
	    if( isset( $row[ 'name' ] ) ) {
	    	if( $row[ 'ruler_id' ] == $_SESSION[ 'ruler_id' ] ) {
	    		$target = "<a href=\"../villages/village.php?name=" . $row[ 'name' ] . "\">$colour" . $row[ 'name' ] . "</strong></a> ";
	    	}
	    	else {
				$target = $row[ 'name' ] . " ";
	    	}
	    }
	    else if( isset( $row[ 'ruler_id' ] ) && $row[ 'ruler_id' ] == -1 ) {
	    	$target = translate( "Independent village" ) . " ";
	    }
		$target = $target . "<a href=\"map.php?x=" . ( $target_x - 20 ) . "&y=" . ( $target_y - 20 ) . "\">" .
			"$colour(" . $target_x . ", " . $target_y . ")</strong></a>";

		$direction = get_army_direction( mysql_result( $result, $i, 'army_id' ) );
		$query = "SELECT name FROM ruler WHERE ruler_id = " . mysql_result( $result, $i, "ruler_id" );
		$result2 = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
		$row2 = mysql_fetch_array( $result2, MYSQL_ASSOC );
        print( "<td>$colour" . $army_name . "</strong></td>" );
		
		if( mysql_result( $result, $i, "ruler_id" ) == -1 ) {
	        print( "<td>$colour" . translate( 'Independent Forces' ) . "</strong></a></td>" );
		}
		else {
	        print( "<td><a href=\"../overview/ruler.php?name=" . $row2[ 'name' ] . "\">$colour" . $row2[ 'name' ] . "</strong></a></td>" );
		}
       	print( "<td>$faction_tag</td>" );
        print( "<td>$colour$target</strong></td>" );
        print( "<td align=\"right\">$colour" . number_format( $size ) . "</strong></td>" );
        print( "<td>$colour" . $direction . "</strong></td></tr>\r\n" );
        $i ++;
    }
    print( "</table>\r\n" );
}
else {
	print( "<div class=\"blok\">\r\n" );
	if( $type == '') {
		print( translate( "We have no reports on any military activity at the moment." ) . "<br />\r\n" );
	}
	else {
		print( translate( "We have no reports on any moving armies." ) . "<br />\r\n" );
	}
	print( "</div>\r\n" );    
}
?>
<?php
aos_footer();
?>
